package org.zw.store.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.zw.store.common.exception.BusinessException;
import org.zw.store.common.exception.NotAuthenticationException;
import org.zw.store.common.exception.SysException;

@Slf4j
@RestControllerAdvice//此注解表示，本类专门拦截注解为@RestController对象中API接口方法
public class StoreApiAdvice {

    @ExceptionHandler(SysException.class)//参数为SysException.class，表示专门处理参数为SysException异常
    public Result handleSysException(SysException e){
        log.error(e.getMessage(),e);
        return Result.ERR(Result.CODE_ERR_SYS,"系统升级中...");
    }

    @ExceptionHandler(BusinessException.class)//参数为BusinessException.class，表示专门处理参数为BusinessException异常
    public Result handleBusinessException(BusinessException e){
        log.error(e.getMessage(),e);
        return Result.ERR(Result.CODE_ERR_BUSINESS,e.getMessage());
    }

    @ExceptionHandler(NotAuthenticationException.class)//参数为 NotAuthenticationException.class，表示专门处理参数为NotAuthenticationException异常
    public Result handleNotAuthenticationException(NotAuthenticationException e){
        log.error(e.getMessage(),e);
        return Result.ERR(Result.CODE_ERR_NOLOGIN,e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        log.error(e.getMessage(),e);
        return Result.ERR(Result.CODE_ERR_SYS,"系统错误");
    }
}
